package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.annotation.Log;
import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.form.AddUserForm;
import com.yuantu.judiciary.model.form.ModifyUserForm;
import com.yuantu.judiciary.model.form.SearchEnterFrom;
import com.yuantu.judiciary.model.form.SearchEnterUserFrom;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.IOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * 组织管理模块
 *
 * @author syw
 * @since 2023-08-12
 **/
@RestController
@RequestMapping(value = "/organization")
@Api(tags = "组织管理模块")
@RequiredArgsConstructor
public class OrganizationController extends BaseController {

    private final IOrganizationService organizationService;

    @ApiOperation(value = "管理端-获取企业列表")
    @PostMapping("/search/enter/page")
    public Response<PageVO<QueryEnterVO>> searchEnterPage(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                          @RequestBody SearchEnterFrom form) {
        return ResponseFactory.okResponse(organizationService.searchEnterPage(form, pageable));
    }

    @ApiOperation(value = "管理端-获取企业用户列表")
    @PostMapping("/search/enter-user/page")
    public Response<PageVO<QueryEnterUserVO>> searchEnterUserPage(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                  @RequestBody @Validated SearchEnterUserFrom form) {
        return ResponseFactory.okResponse(organizationService.searchEnterUserPage(form, pageable));
    }

    @ApiOperation(value = "管理端-获取个人列表")
    @PostMapping("/search/single/page")
    public Response<PageVO<QuerySingleVO>> searchSinglePage(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                            @RequestBody @Validated SearchEnterFrom form) {
        return ResponseFactory.okResponse(organizationService.searchSinglePage(form, pageable));
    }

    @ApiOperation(value = "小程序-获取用户列表")
    @PostMapping("/get/user-list")
    public Response<List<AccountListVO>> getUserList() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(organizationService.getUserList(accountId));
    }

    @ApiOperation(value = "小程序-添加用户")
    @PostMapping("/add/user")
    public Response<Boolean> addUser(@RequestBody @Validated AddUserForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(organizationService.addUser(accountId, form));
    }

    @ApiOperation(value = "小程序-修改用户")
    @PostMapping("/modify/user")
    public Response<Boolean> modifyUser(@RequestBody @Validated ModifyUserForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(organizationService.modifyUser(accountId, form));
    }

    @ApiOperation(value = "小程序-删除用户")
    @PostMapping("/delete/user")
    public Response<Boolean> deleteUser(@RequestBody @NotEmpty List<Long> id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(organizationService.deleteUser(accountId, id));
    }

    @Log(title = "导入来源企业")
    @ApiOperation(value = "导入来源企业")
    @PostMapping("/upload")
    public Response<Boolean> uploadSourceEnterInfos(@RequestBody MultipartFile file, @RequestParam Long organizationSourceId) {

        return ResponseFactory.okResponse(organizationService.uploadSourceEnterInfos(file, organizationSourceId));
    }


    @Log(title = "导入企业查询使用次数")
    @ApiOperation(value = "导入企业查询使用次数")
    @PostMapping("/upload/use/time")
    public Response<Boolean> uploadEnterUseTimes(@RequestBody MultipartFile file, @RequestParam String codeInfo) {

        return ResponseFactory.okResponse(organizationService.uploadEnterUseTimes(file, codeInfo));
    }


    @ApiOperation(value = "下载导入来源企业模板")
    @PostMapping("/download/template")
    public void downloadSourceEnterTemplate(HttpServletResponse response) {

        organizationService.downloadSourceEnterTemplate(response);
    }


    //没有使用
    @ApiOperation(value = "查询企业来源有关企业信息")
    @PostMapping("/source/list")
    public Response<PageVO<OrgSourceOrganizationVO>> querySourceEnterInfos(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                           @RequestBody OrgSourceOrganizationQueryVO query) {

        return ResponseFactory.okResponse(organizationService.querySourceEnterInfos(query, pageable));
    }

    @Log(title = "新增/修改企业来源有关企业信息")
    @ApiOperation(value = "新增/修改企业来源有关企业信息")
    @PostMapping("/source/update")
    public Response<Boolean> updateSourceEnterInfo(@RequestBody @Validated OrgSourceOrganizationVO orgSourceOrganization) {

        return ResponseFactory.okResponse(organizationService.updateSourceEnterInfo(orgSourceOrganization));
    }



    @ApiOperation(value = "下载未导入的来源企业")
    @PostMapping("/download")
    public void downloadSourceEnterInfos(@RequestParam Long organizationSourceId, @RequestBody MultipartFile file, HttpServletResponse response) {
        organizationService.downloadSourceEnterInfos(organizationSourceId, file, response);
    }




}
